package com.ltmonitor.web.controller;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ltmonitor.service.IBaseService;
import com.ltmonitor.web.util.JsonMessage;

public abstract class PersistenceAction<K extends Serializable, T extends Serializable>
		extends PaginateAction {
	
	protected String EditViewName = "edit";
	
	protected String SaveViewName = "edit";
	
	@Autowired
	protected IBaseService<K,T>  baseService;
	
	
	//protected abstract IBaseService<K, T> getBaseService();

	/**
	 * 查看详细信息
	 * 
	 * @return
	 */
	@RequestMapping("/view.action")
	public ModelAndView view(Model model,
			@RequestParam(value = "entityId", required = false) K id) {

		ModelAndView m = new ModelAndView();
		m.setViewName(EditViewName);
		try {
			if (id != null) {
				T t = baseService.load(id);
				if(t == null)
				{

					Map entity = new HashMap();
					entity.put("entityId", 0);
					m.addObject("entity", entity);
				}else
				m.addObject("entity", t);
			}else
			{
				Map entity = new HashMap();
				entity.put("entityId", 0);
				m.addObject("entity", entity);
				
			}
			//m.addObject("message", "保存成功");

		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			m.addObject("发生错误:"+ex.getMessage());
		}

		return m;
	}

	@RequestMapping( "/save.action")
	public ModelAndView save(HttpServletRequest request, Model model,
			 T entity) {
		ModelAndView m = new ModelAndView();
		m.setViewName(EditViewName);
		try {

			this.baseService.saveOrUpdate(entity);
			m.addObject("entity", entity);
			m.addObject("message", "保存成功");

		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			m.addObject("message",ex.getMessage());
		}

		return m;
	}
	/**
	 * 彻底从数据库中删除
	 * @param request
	 * @param model
	 * @param id 实体类ID
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/delete.action")
	public JsonMessage delete(HttpServletRequest request, Model model,
			@RequestParam(value = "entityId") K id) {

		try {
			this.baseService.deleteById(id);
			return jsonSuccess("记录已被成功刪除!");

		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			return jsonFail(ex.getMessage());
		}

	}
	/**
	 * 假删除，通过更新数据库的删除标记
	 * @param request
	 * @param model
	 * @param id 实体类ID
	 * @return
	 */
	@ResponseBody
	@RequestMapping( value = "/fakeDelete.action")
	public JsonMessage fakeDelete(HttpServletRequest request, Model model,
			@RequestParam(value = "entityId") K id) {

		try {
			this.baseService.deleteFakeById(id);
			return jsonSuccess("记录已被成功刪除!");

		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			return jsonFail(ex.getMessage());
		}

	}
	

}
